package com.account.core.controller;

import com.account.common.core.controller.BaseController;
import com.account.common.core.domain.AjaxResult;
import com.account.common.core.page.TableDataInfo;
import com.account.common.utils.poi.ExcelUtil;
import com.account.core.domain.Account;
import com.account.core.dto.AccountView;
import com.account.core.dto.UpdatePasswordRequest;
import com.account.core.dto.UserCreateRequest;
import com.account.core.service.IAccountService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 账号Controller
 *
 * @author ylstech
 * @date 2025-07-07
 */
@RestController
@RequestMapping("/account")
public class AccountController extends BaseController {

    @Resource
    private IAccountService accountService;

    /**
     * 查询账号列表
     */
    @PreAuthorize("@ss.hasPermi('account:list')")
    @GetMapping("/list")
    public TableDataInfo list(Account account) {
        startPage();
        List<Account> list = accountService.selectAccountList(account);
        return getDataTable(list);
    }

    /**
     * 导出账号列表
     */
    @PreAuthorize("@ss.hasPermi('account:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountView account) {
        List<Account> list = null;
        if (account.getIds() != null && account.getIds().length > 0) {
            list = accountService.selectAccountByIds(account.getIds());
        } else {
            list = accountService.selectAccountList(account);
        }
        ExcelUtil<Account> util = new ExcelUtil<Account>(Account.class);
        util.exportExcel(response, list, "账号数据");
    }

    /**
     * 获取账号详细信息
     */
    @PreAuthorize("@ss.hasPermi('account:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(accountService.selectAccountById(id));
    }

    /**
     * 新增账号
     */
    @PreAuthorize("@ss.hasPermi('account:add')")
    @PostMapping
    public AjaxResult add(@RequestBody @Valid UserCreateRequest req) {
        accountService.insertAccount(req);
        return AjaxResult.success();
    }

    /**
     * 修改账号
     */
    @PreAuthorize("@ss.hasPermi('account:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid Account account) {
        accountService.updateAccount(account);
        return AjaxResult.success();
    }

    /**
     * 重置密码
     *
     * @param req
     * @return
     */
    @PreAuthorize("@ss.hasPermi('account:edit')")
    @PostMapping("/password")
    public AjaxResult resetPassword(@Validated @RequestBody UpdatePasswordRequest req) {
        accountService.resetPassword(req);
        return AjaxResult.success();
    }

    /**
     * 删除账号
     */
    @PreAuthorize("@ss.hasPermi('account:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        accountService.deleteAccountByIds(ids);
        return AjaxResult.success();
    }
}
